#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <wait.h>

#include <iostream>

void promoteToFore(){
    int ch;
    switch (ch=fork())
    {
    case -1: // error
        /* code */
        std::cout<<"Error"<<std::endl;
        return;
    case 0: // son
        sleep(2);
        std::cout<<"Fg:"<<tcgetpgrp(STDIN_FILENO)<<std::endl;
        std::cout<<"Son:"<<getpid()<<"\nI'm in foreground now"<<std::endl;
        execlp("vim","vim","main.cpp",NULL);
        break;
    default: // father
        std::cout<<"I'm Father"<<std::endl;
        setpgid(ch,ch); // it works
        tcsetpgrp(STDIN_FILENO,ch); // set as the fg
        std::cout<<"Fg:"<<tcgetpgrp(STDIN_FILENO)<<"\tFather:"<<getpid()<<std::endl;
        std::cout<<"Sleep Ends"<<std::endl;
        wait(0);
        break;
    }
}

int main() {
    promoteToFore();
    return 0;
}